package edu.ccut.saturn.workflow.agent;

import java.sql.Connection;
import java.util.HashMap;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.WorkFlowTools;

public class SetAgentAll extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		HashMap<String,String> workFlowParamMap = (HashMap<String,String>)parameterContext.getParameterValue(0);
		String processids = (String)parameterContext.getParameterValue(1);
		Connection conn = parameterContext.getConnection();
		
		SaturnData agentInst = WorkFlowTools.getAgentInstance();
		agentInst.set("agent_to", workFlowParamMap.get("agent_to"));
		agentInst.set("agent_from", workFlowParamMap.get("agent_from"));
		agentInst.set("agent_to_type", workFlowParamMap.get("agent_to_type"));
		agentInst.set("start_time", workFlowParamMap.get("start_time"));
		agentInst.set("end_time", workFlowParamMap.get("end_time"));
		agentInst.set("agent_type", "ALL");
		agentInst.set("agent_reason", workFlowParamMap.get("agent_reason"));
		//插入代理表
		Operation.dataBase().insert(agentInst, conn);
		
		if(!"".equals(processids)){
			String[] expProcessList = processids.split(",");			
			for(int i=0;i<expProcessList.length;i++){
				SaturnData agentitemInst = WorkFlowTools.getAgentitemInstance();
				agentitemInst.set("itemtype", "PROCESS");
				agentitemInst.set("itemid", expProcessList[i]);
				agentitemInst.set("agentid", agentInst.get("agent_id")+"");
				//插入agentitemInst 注：agentitemInst表中，完全代理时存入的是例外流程，部分代理时存入的是有效的流程和活动
				Operation.dataBase().insert(agentitemInst, conn);
			}
		}	
	}
}
